import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { getRemoveList, editRecord, addRecord } from '../services/users';
import { message } from 'antd'
import {ISingleUser} from './data'

export interface IUserState {
    data: ISingleUser[],
    meta: {
        total: number
        per_page: number
        page: number
    }
}

interface IUserModel {
    namespace: 'users'
    state: IUserState
    reducers: {
        getList: Reducer<IUserState>
    }
    effects: {
        getRemove: Effect
        edit: Effect
        add: Effect
    }
    subscriptions: {
        setup: Subscription
    }
}


const UserModel: IUserModel = {
    namespace: 'users',
    state: {
        data: [],
        meta: {
            total: 0,
            per_page: 10,
            page: 1
        }
    },
    reducers: {
        getList(state, action) {
            return action.payload
        }
    },
    effects: {
        *getRemove({payload: {page, per_page}}, effects) {
            const data = yield effects.call(getRemoveList, { page, per_page})
            if (data) {
                yield effects.put({
                    type: 'getList',
                    payload: data
                })
            }
        },
        *edit({payload: {id, values}}, effects) {
            const data = yield effects.call(editRecord, id, values)
            console.log('编辑后的结果', data)
            // 调用 getRemove 方法重新获取数据
            if (data) {
                message.success('编辑成功！')
                const { page, per_page } = yield effects.select((state: any) => state.users.meta )
                yield effects.put({
                    type: 'getRemove',
                    payload: {
                        page,
                        per_page
                    }
                })
            }else {
                message.error('编辑失败！')
            }
        },
        *add({payload: {values}}, effects) {
            console.log('payload', values)
              
            const data = yield effects.call(addRecord, values)
            // 调用 getRemove 方法重新获取数据
            if (data) {
                message.success('添加成功！')
                const { page, per_page } = yield effects.select((state: any) => state.users.meta )
                yield effects.put({
                    type: 'getRemove',
                    payload: {
                        page,
                        per_page
                    }
                })
            }else {
                message.error('添加失败！')
            }
        }
    },
    subscriptions: {
        setup({ dispatch, history }, done) {
            return history.listen((location, action) => {
                if(location.pathname === '/users' || location.pathname === '/my') {
                    // 监听路由的改变，当路由为 '/users' 时，发送 action 获取数据，返回到页面。
                    dispatch({
                        type: 'getRemove',
                        payload: {
                            page: 1,
                            per_page: 5
                        }
                    })
                }
            })
        }
    }
};
export default UserModel;